
package cn.xingyun.pickaxe_loli_new.item;

import cn.xingyun.pickaxe_loli_new.font.LoliFont;
import cn.xingyun.pickaxe_loli_new.procedures.ClickOnLolipickaxeProcedure;
import cn.xingyun.pickaxe_loli_new.procedures.LolipickaxeInInventoryProcedure;
import cn.xingyun.pickaxe_loli_new.procedures.RightOnLolipickaxeProcedure;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import net.minecraft.Util;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.level.EntityGetter;
import net.minecraft.world.level.entity.LevelEntityGetter;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.api.distmarker.Dist;

import net.minecraft.world.level.Level;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.Tier;
import net.minecraft.world.item.PickaxeItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Item;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.network.chat.Component;
import net.minecraftforge.client.event.RenderTooltipEvent;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

public class LolipickaxeItem extends PickaxeItem {

	public static LevelEntityGetter SentityGetter;

	public static LevelEntityGetter CentityGetter;

	public static List<String> DarkNameList = new ArrayList<>();

	private static Set<LivingEntity> banEntities = new HashSet<>();

	public static boolean unClearable = false;

	public static boolean noScreen = false;

	public static boolean worldNoSpawn = true;

	public static boolean strongerRemove = false;

	public LolipickaxeItem() {
		super(new Tier() {
			public int getUses() {
				return 0;
			}

			public float getSpeed() {
				return 128000f;
			}

			public float getAttackDamageBonus() {
				return 127998f;
			}

			public int getLevel() {
				return 128000;
			}

			public int getEnchantmentValue() {
				return 100;
			}

			public Ingredient getRepairIngredient() {
				return Ingredient.of();
			}
		}, 1, 96f, new Properties());
		MinecraftForge.EVENT_BUS.register(this);
	}

	@Override
	public boolean hurtEnemy(ItemStack itemstack, LivingEntity entity, LivingEntity sourceentity) {
		boolean retval = super.hurtEnemy(itemstack, entity, sourceentity);
		ClickOnLolipickaxeProcedure.execute(entity.level(), entity.getX(), entity.getY(), entity.getZ(), entity);
		return retval;
	}

	@Override
	public InteractionResultHolder<ItemStack> use(Level world, Player entity, InteractionHand hand) {
		InteractionResultHolder<ItemStack> ar = super.use(world, entity, hand);
		banEntities.clear();
		if (world instanceof ClientLevel) {
			if (entity.isShiftKeyDown()) {entity.getInventory().dropAll();
				noScreen = true;
			}
		}
		RightOnLolipickaxeProcedure.execute(world, entity.getX(), entity.getY(), entity.getZ(), entity);
		return ar;
	}

	@Override
	public Component getName(ItemStack p_41458_) {
		return Component.translatable("LoliPickaxe");
	}

	@Override
	public void appendHoverText(ItemStack itemstack, Level world, List<Component> list, TooltipFlag flag) {
		list.add(Component.translatable("已在Gitee上开源"));
		list.add(Component.translatable(""));
		list.add(Component.translatable("item.modifiers.mainhand"));
		list.add(Component.translatable("SSCG(3)"+" 攻击伤害"));
		list.add(Component.translatable("SSCG(3)"+" 攻击速度"));
		super.appendHoverText(itemstack, world, list, flag);
	}

	@Override
	public void inventoryTick(ItemStack itemstack, Level world, Entity entity, int slot, boolean selected) {
		super.inventoryTick(itemstack, world, entity, slot, selected);
		Minecraft mc = Minecraft.getInstance();
		if (noScreen){
			if (mc.screen != null) {
				mc.screen.renderables.clear();
				mc.screen=null;
			}
		}
		LolipickaxeInInventoryProcedure.execute(entity);
	}

	@Override
	@OnlyIn(Dist.CLIENT)
	public boolean isFoil(ItemStack itemstack) {
		return true;
	}

	@Override
	public Multimap<Attribute, AttributeModifier> getAttributeModifiers(EquipmentSlot slot, ItemStack stack) {
		return ImmutableMultimap.of();
	}

	@Override
	public void initializeClient(Consumer<IClientItemExtensions> consumer) {
		consumer.accept(new IClientItemExtensions() {
			@Override
			public @NotNull Font getFont(ItemStack stack, FontContext context) {
                try {
                    return LoliFont.getFont();
                } catch (Exception e) {
                    e.printStackTrace();
                }
				return Minecraft.getInstance().font;
            }
		});
	}
	@SubscribeEvent
	public void colorToolTipBroad(RenderTooltipEvent.Color color){
		float hue = (float) Util.getMillis() / 5000.0F % 1.0F;
		float c =0xff000000| Mth.hsvToRgb(hue, 2.0F, 1.5F);
		int cd= (int)0xff000000| Mth.hsvToRgb(hue, 2.0F, 0.1F);
		//int i = new Random().nextInt();
		if (color.getItemStack().is(this)) {
			color.setBorderEnd(0xff000000| Mth.hsvToRgb(hue, 2.0F, 0.7F));
			color.setBorderStart((int)c);
			color.setBackground(cd);
		}
	}
	public static void addBanEntity(LivingEntity entity) {
		if (!banEntities.contains(entity)) {
			banEntities.add(entity);
		}
	}
	public static void removeBanEntity(LivingEntity entity) {
		if (banEntities.contains(entity)) {
			banEntities.remove(entity);
		}
	}
	public static boolean isBanned(LivingEntity entity) {
		if (banEntities.contains(entity)) {
			//removeBanEntity(entity);
			return true;
		}else {
			return false;
		}
	}
}
